//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.Menu.Item;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Gallery.LayoutParams;

public class TrafficmanFileServerNotepad extends ListActivity {
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;

	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;

	private TrafficmanFileServerNotesDbAdapter mDbHelper;

	private SQLiteDatabase db;
	private SQLiteDatabase db_files;

	private int int_intent_active;
	private int int_selected_id;
	private String mainmenu_text;
	private String title_text;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Window w = getWindow();
		w.requestFeature(Window.FEATURE_LEFT_ICON);
		
		setContentView(R.layout.files_server_list);

		LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
		TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
		TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
		get_JobView_Screen(); // set JobView2 background
		int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
				null);
		tmpLayout.setBackground(resID);

		//setup list of files
		mDbHelper = new TrafficmanFileServerNotesDbAdapter(this);
		mDbHelper.open();
		fillData();
		
		//setup title bar
		title_text = getString(R.string.jv_server_files);
		this.setTitle(title_text);
		w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.network_01);
	}

	private void get_JobView_Screen() {
		TrafficmanScreens mScreens = new TrafficmanScreens(this);
	}

	private void fillData() {
		// Get all of the rows from the database and create the item list
		Cursor notesCursor = mDbHelper.fetchAllNotes();
		startManagingCursor(notesCursor);
		String[] from = new String[] { TrafficmanFileServerNotesDbAdapter.KEY_JV_SERVER };
		int[] to = new int[] { R.id.text1 };
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.files_server_notes_row, notesCursor, from, to);
		setListAdapter(notes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, R.string.jv_server_menu_insert,
				R.drawable.network_03);
		menu.add(0, DELETE_ID, R.string.jv_server_menu_delete,
				R.drawable.network_04);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		switch (item.getId()) {
		case INSERT_ID:
			createNote();
			return true;
		case DELETE_ID:
			mDbHelper.deleteNote(getListView().getSelectedItemId());
			fillData(); 
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void createNote() {
		int_intent_active = 1;
		Intent intent = new Intent(this, TrafficmanFileServerNoteEdit.class);
		startSubActivity(intent, ACTIVITY_CREATE);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		int_intent_active = 1;

		TrafficmanConstants.SAVE_CLICK = 1;
		setResult(RESULT_OK);

		Intent i = new Intent();
		i.putExtra(TrafficmanFileServerNotesDbAdapter.KEY_ROWID, id);
		i.setClass(TrafficmanFileServerNotepad.this, TrafficmanFileServerNoteEdit.class);
		startActivity(i);
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			String data, Bundle extras) {
		super.onActivityResult(requestCode, resultCode, data, extras);
		fillData();
	}


	@Override
	protected void onPause() {
		if (int_intent_active == 1) {
			// do nothing
			int_intent_active = 0;
			super.onPause();
		} else {
			super.onPause();
			Intent intent = new Intent();
			intent.setClass(TrafficmanFileServerNotepad.this, TrafficmanMainActivity.class);
			startActivity(intent);
			finish();
		}
	}

	@Override
	
	protected void onResume() {
		if (int_intent_active == 0)	{
			super.onResume();
			//populateFields();
			fillData();
		}
	}


	

}
